package com.lvwenguo.controller.user;

import cn.itcast.commons.CommonUtils;
import com.lvwenguo.model.User;
import com.lvwenguo.service.UserServiceInter;
import com.lvwenguo.service.serviceImple.UserServiceImple;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    UserServiceInter userServiceInter=new UserServiceImple();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");

        //表示查询操作
        if(Integer.parseInt(req.getParameter("action"))==0){
            req.setAttribute("user",userServiceInter.selectOnt(req.getParameter("name")));
            req.getRequestDispatcher("/view/user/user_list.jsp").forward(req,resp);

        }

        //表示是添加操作
        else if(Integer.parseInt(req.getParameter("action"))==1){
            User user = CommonUtils.toBean(req.getParameterMap(), User.class);
                       int i = userServiceInter.AddUser(user);

          resp.sendRedirect("http://localhost:8080/supermarket/MenuJumpServlet?method=user_list");

        }
        //表示是更新操作
        else if(Integer.parseInt(req.getParameter("action"))==2){
            User user = CommonUtils.toBean(req.getParameterMap(), User.class);
            int i = userServiceInter.updateUsre(user);
            resp.sendRedirect("http://localhost:8080/supermarket/MenuJumpServlet?method=user_list");

        }
        //表示是删除操作
        else if(Integer.parseInt(req.getParameter("action"))==3){
            int i = userServiceInter.deleteUser(Integer.parseInt(req.getParameter("id")));
            resp.sendRedirect("http://localhost:8080/supermarket/MenuJumpServlet?method=user_list");

        }
        //数据回显功能
       else if(Integer.parseInt(req.getParameter("action"))==4){
           req.setAttribute("user",userServiceInter.userData(Integer.parseInt(req.getParameter("id"))));
            req.getRequestDispatcher("/view/user/user_modify.jsp").forward(req,resp);
        }
       //用于上传头像
        else if(Integer.parseInt(req.getParameter("action"))==5) {

            if (ServletFileUpload.isMultipartContent(req)) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
                try {
                    List<FileItem> list = servletFileUpload.parseRequest(req);
                    for (FileItem fileItem : list) {
                        if (fileItem.isFormField()) {
                            System.out.println("表单项的name属性" + fileItem.getFieldName());
                            System.out.println("表单项的value属性" + fileItem.getString("utf-8"));

                        } else {
                            System.out.println("表单项的name属性" + fileItem.getFieldName());
                            System.out.println("上传文件的文件名" + fileItem.getName());
                            fileItem.write(new File("E://IDEAMaven/maven04/web/images/" + fileItem.getName()));
                            String imgUrl = "http://localhost:8080/supermarket/images/" + fileItem.getName();
                            userServiceInter.updateImgUrl(req.getSession().getAttribute("sessionname").toString(),imgUrl);
                            resp.sendRedirect("http://localhost:8080/supermarket/index.jsp");

                        }
                    }
                } catch (FileUploadException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
